#include "face_app.h"
#include "event_app.h"
#include "basic_app.h"
#include "sound_app.h"
#include "memroy_app.h"
#include "finger_app.h"
#include "irq_contorl.h"
#include "gtx31x_board.h"
#include "protocol_api.h"
#include "cardholder_api.h"
#include "device_parameter.h"

#if _KEYBOARD_FUNCTION_ENABLE_

/**
 * @brief 配置 SDA 管脚功能
 *
 */
static void Gtx31xSdaPinConfiguration(void);

/**
 * @brief 配置 SCL 管脚功能
 *
 */
static void Gtx31xSclPinConfiguration(void);

/**
 * @brief 配置中断管脚功能
 *
 */
static void Gtx31xIrqPinConfiguration(void);

/**
 * @brief 设置 SDA 管脚为高电平
 *
 */
static void Gtx31xSetSdaHighLevel(void);

/**
 * @brief 设置 SDA 为低电平
 *
 */
static void Gtx31xSetSdaLowLevel(void);

/**
 * @brief 设置 SCL 管脚为高电平
 *
 */
static void Gtx31xSetSclHighLevel(void);

/**
 * @brief 设置 SCL 管脚为低电平
 *
 */
static void Gtx31xSetSclLowLevel(void);

/**
 * @brief 读取 SDA 数据线管脚电平状态
 *
 * @return bool - 管脚电平状态（true：高电平 false：低电平）
 */
static bool Gtx31xReadSdaPinLevel(void);

/**
 * @brief 将SDA设置为输入模式
 *
 */
static void Gtx31xSetSdaInput(void);

/**
 * @brief 将SDA设置为输出模式
 *
 */
static void Gtx31xSetSdaOutput(void);

/**
 * @brief 使能中断
 *
 */
static void Gtx31xEnableIrq(void);

/**
 * @brief 失能中断
 *
 */
static void Gtx31xDisableIrq(void);

/**
 * @brief 获取中断管脚电平状态
 *
 * @return bool - 中断管脚电平状态（true：高电平 false：低电平）
 */
static bool Gtx31xGetIrqPinLevel(void);

/**
 * @brief 获取中断管脚中断标志
 *
 * @return bool - 中断使能标志（true：触发 false：静默）
 */
static bool Gtx31xGetIrqFlagBit(void);

/**
 * @brief 清除中断管脚标志
 *
 */
static void Gtx31xClearIrqFlagBit(void);

/**
 * @brief 中断回调
 *
 * @return bool - 中断触发状态（true：触发 false：静止）
 */
static bool Gtx31xTiggerIrqCallback(void);

/**
 * @brief 按键事件回调
 *
 */
static void Gtx31xEventHandlerCallback(void);

/**
 * @brief 事件处理中心
 *
 */
static uint16_t Gtx31xEventTaskCallback(uint8_t TaskId, uint16_t Events);

static uint8_t Gtx31xEventTaskId = 0;
static bool Gtx31xUserKeyscanfEn = false;
static IrqControl_t Gtx31xIrqControl = {0};
static EventHandlerTable_t Gtx31xEventHandlerTable = {0};
static Gtx31xExternalInterfaces_t Gtx31xExternalInterfaces = {0};

static void Gtx31xSdaPinConfiguration(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ModeCfg(GTX31X_IIC_SDA_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_ModeCfg(GTX31X_IIC_SDA_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOA_ModeCfg(GTX31X_IIC_SDA_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ModeCfg(GTX31X_IIC_SDA_PIN, GPIO_ModeOut_PP_5mA);
#endif

  Gtx31xSetSdaPinLevel(true);
}

static void Gtx31xSclPinConfiguration(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ModeCfg(GTX31X_IIC_SCL_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_ModeCfg(GTX31X_IIC_SCL_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOA_ModeCfg(GTX31X_IIC_SCL_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ModeCfg(GTX31X_IIC_SCL_PIN, GPIO_ModeOut_PP_5mA);
#endif

  Gtx31xSetSclPinLevel(true);
}

static void Gtx31xIrqPinConfiguration(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ModeCfg(GTX31X_IRQ_PIN, GPIO_ModeIN_PU);
  GPIOB_ITModeCfg(GTX31X_IRQ_PIN, GPIO_ITMode_FallEdge);

  PFIC_EnableIRQ(GPIO_B_IRQn);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_ModeCfg(GTX31X_IRQ_PIN, GPIO_ModeIN_PU);
  GPIOB_ITModeCfg(GTX31X_IRQ_PIN, GPIO_ITMode_FallEdge);

  PFIC_EnableIRQ(GPIO_B_IRQn);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOA_ModeCfg(GTX31X_IRQ_PIN, GPIO_ModeIN_PU);
  GPIOA_ITModeCfg(GTX31X_IRQ_PIN, GPIO_ITMode_FallEdge);

  PFIC_EnableIRQ(GPIO_A_IRQn);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ModeCfg(GTX31X_IRQ_PIN, GPIO_ModeIN_PU);
  GPIOB_ITModeCfg(GTX31X_IRQ_PIN, GPIO_ITMode_FallEdge);

  PFIC_EnableIRQ(GPIO_B_IRQn);
#endif

  AddIrqNewTable(&Gtx31xIrqControl, Gtx31xTiggerIrqCallback);
  PWR_PeriphWakeUpCfg(ENABLE, RB_SLP_GPIO_WAKE, Long_Delay);
}

static void Gtx31xSetSdaHighLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_SetBits(GTX31X_IIC_SDA_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_SetBits(GTX31X_IIC_SDA_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOA_SetBits(GTX31X_IIC_SDA_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_SetBits(GTX31X_IIC_SDA_PIN);
#endif
}

static void Gtx31xSetSdaLowLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ResetBits(GTX31X_IIC_SDA_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_ResetBits(GTX31X_IIC_SDA_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOA_ResetBits(GTX31X_IIC_SDA_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ResetBits(GTX31X_IIC_SDA_PIN);
#endif
}

static void Gtx31xSetSclHighLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_SetBits(GTX31X_IIC_SCL_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_SetBits(GTX31X_IIC_SCL_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOA_SetBits(GTX31X_IIC_SCL_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_SetBits(GTX31X_IIC_SCL_PIN);
#endif
}

static void Gtx31xSetSclLowLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ResetBits(GTX31X_IIC_SCL_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_ResetBits(GTX31X_IIC_SCL_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOA_ResetBits(GTX31X_IIC_SCL_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ResetBits(GTX31X_IIC_SCL_PIN);
#endif
}

static bool Gtx31xReadSdaPinLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  if (GPIOB_ReadPortPin(GTX31X_IIC_SDA_PIN) == GTX31X_IIC_SDA_PIN)
    return true;

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  if (GPIOB_ReadPortPin(GTX31X_IIC_SDA_PIN) == GTX31X_IIC_SDA_PIN)
    return true;

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  if (GPIOA_ReadPortPin(GTX31X_IIC_SDA_PIN) == GTX31X_IIC_SDA_PIN)
    return true;

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  if (GPIOB_ReadPortPin(GTX31X_IIC_SDA_PIN) == GTX31X_IIC_SDA_PIN)
    return true;
#endif

  return false;
}

static void Gtx31xSetSdaInput(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ModeCfg(GTX31X_IIC_SDA_PIN, GPIO_ModeIN_PU);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_ModeCfg(GTX31X_IIC_SDA_PIN, GPIO_ModeIN_PU);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOA_ModeCfg(GTX31X_IIC_SDA_PIN, GPIO_ModeIN_PU);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ModeCfg(GTX31X_IIC_SDA_PIN, GPIO_ModeIN_PU);
#endif
}

static void Gtx31xSetSdaOutput(void)
{
#if _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_ModeCfg(GTX31X_IIC_SDA_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOA_ModeCfg(GTX31X_IIC_SDA_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ModeCfg(GTX31X_IIC_SDA_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ModeCfg(GTX31X_IIC_SDA_PIN, GPIO_ModeOut_PP_5mA);
#endif
}

static void Gtx31xEnableIrq(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  R16_PB_INT_EN |= GTX31X_IRQ_PIN;
  GPIOB_ClearITFlagBit(GTX31X_IRQ_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  R16_PB_INT_EN |= GTX31X_IRQ_PIN;
  GPIOB_ClearITFlagBit(GTX31X_IRQ_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  R16_PA_INT_EN |= GTX31X_IRQ_PIN;
  GPIOA_ClearITFlagBit(GTX31X_IRQ_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  R16_PB_INT_EN |= GTX31X_IRQ_PIN;
  GPIOB_ClearITFlagBit(GTX31X_IRQ_PIN);
#endif
}

static void Gtx31xDisableIrq(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  R16_PB_INT_EN &= ~GTX31X_IRQ_PIN;
  GPIOB_ClearITFlagBit(GTX31X_IRQ_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  R16_PB_INT_EN &= ~GTX31X_IRQ_PIN;
  GPIOB_ClearITFlagBit(GTX31X_IRQ_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  R16_PA_INT_EN &= ~GTX31X_IRQ_PIN;
  GPIOA_ClearITFlagBit(GTX31X_IRQ_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  R16_PB_INT_EN &= ~GTX31X_IRQ_PIN;
  GPIOB_ClearITFlagBit(GTX31X_IRQ_PIN);
#endif
}

static bool Gtx31xGetIrqPinLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  if (GPIOB_ReadPortPin(GTX31X_IRQ_PIN) == GTX31X_IRQ_PIN)
    return true;

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  if (GPIOB_ReadPortPin(GTX31X_IRQ_PIN) == GTX31X_IRQ_PIN)
    return true;

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  if (GPIOA_ReadPortPin(GTX31X_IRQ_PIN) == GTX31X_IRQ_PIN)
    return true;

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  if (GPIOB_ReadPortPin(GTX31X_IRQ_PIN) == GTX31X_IRQ_PIN)
    return true;
#endif

  return false;
}

static bool Gtx31xGetIrqFlagBit(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  if (GPIOB_ReadITFlagBit(GTX31X_IRQ_PIN) == GTX31X_IRQ_PIN)
    return true;

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  if (GPIOB_ReadITFlagBit(GTX31X_IRQ_PIN) == GTX31X_IRQ_PIN)
    return true;

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  if (GPIOA_ReadITFlagBit(GTX31X_IRQ_PIN) == GTX31X_IRQ_PIN)
    return true;

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  if (GPIOB_ReadITFlagBit(GTX31X_IRQ_PIN) == GTX31X_IRQ_PIN)
    return true;
#endif

  return false;
}

static void Gtx31xClearIrqFlagBit(void)
{
#if  _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ClearITFlagBit(GTX31X_IRQ_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_ClearITFlagBit(GTX31X_IRQ_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOA_ClearITFlagBit(GTX31X_IRQ_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ClearITFlagBit(GTX31X_IRQ_PIN);
#endif
}

static bool Gtx31xTiggerIrqCallback(void)
{
  if (Gtx31xGetIrqFlagBit() == true)
  {
    Gtx31xClearIrqFlagBit();

    Gtx31xUserKeyscanfEn = true;
    return true;
  }

  return false;
}

static void Gtx31xEventHandlerCallback(void)
{
  if (Gtx31xUserKeyscanfEn == true)
  {
    Gtx31xUserKeyscanfEn = false;

    Gtx31xDelayMs(5);
    if (Gtx31xGetIrqPinLevel() == false)
    {
      if (Gtx31xExternalInterfaces.IrqCallback != NULL)
      {
        Gtx31xExternalInterfaces.IrqCallback();
      }
    }
  }
}

static uint16_t Gtx31xEventTaskCallback(uint8_t TaskId, uint16_t Events)
{
  if (TaskId == Gtx31xEventTaskId)
  {
    if (Events & GTX31X_SELF_TEST)
    {
      if (Gtx31xExternalInterfaces.DeviceSelfTestTask != NULL)
      {
        Gtx31xExternalInterfaces.DeviceSelfTestTask();
      }

      return (Events ^ GTX31X_SELF_TEST);
    }

    if (Events & GTX31X_DATA_INTERACTION)
    {
      if (Gtx31xExternalInterfaces.DataInteractionTask != NULL)
      {
        Gtx31xExternalInterfaces.DataInteractionTask();
      }

      return (Events ^ GTX31X_DATA_INTERACTION);
    }

    if (Events & GTX31X_KEYSCANF_LOCKED_TIMER)
    {
      if (Gtx31xExternalInterfaces.KeyboardDisableTimeout != NULL)
      {
        Gtx31xExternalInterfaces.KeyboardDisableTimeout();
      }

      return (Events ^ GTX31X_KEYSCANF_LOCKED_TIMER);
    }
  }

  return 0;
}

void Gtx31xDelayUs(uint16_t Us)
{
  DelayUs(Us);
}

void Gtx31xDelayMs(uint16_t Ms)
{
  DelayMs(Ms);
}

void Gtx31xSetSclPinLevel(bool PinLevel)
{
  (PinLevel == true) ? Gtx31xSetSclHighLevel() : Gtx31xSetSclLowLevel();
}

void Gtx31xSetSdaPinLevel(bool PinLevel)
{
  (PinLevel == true) ? Gtx31xSetSdaHighLevel() : Gtx31xSetSdaLowLevel();
}

bool Gtx31xGetSdaPinLevel(void)
{
  return Gtx31xReadSdaPinLevel();
}

void Gtx31xConfigurationSdaInput(void)
{
  Gtx31xSetSdaInput();
}

void Gtx31xConfigurationSdaOutput(void)
{
  Gtx31xSetSdaOutput();
}

void Gtx31xEnableTouchInterrupts(void)
{
  Gtx31xEnableIrq();
}

void Gtx31xMaskedTouchInterrupts(void)
{
  Gtx31xDisableIrq();
}

uint8_t Gtx31xGetKeyboardSensitivity(void)
{
  return GetKeyboardSensitivity();
}

uint32_t GetKeyScanfTimeout(void)
{
  return GetSearchTimeout() * 1000;
}

void Gtx31xDiableWriteRegisterProtection(void)
{
  PFIC_DisableAllIRQ();
}

void Gtx31xEnableWriteRegisterProtection(void)
{
  PFIC_EnableAllIRQ();
}

void Gtx31xPlayKeypadTone(void)
{
  PlayKeypadToneAudio();
}

void Gtx31xPlayTheKeyEntryErrorSoundEffect(void)
{
  PlayLnvalidKeyValueAudio();
}

void Gtx31xPlayPermissionDeniedAudio(void)
{
  PlayPermissionDeniedAudio();
}

void Gtx31xPlayVerifyAdminPasswordAudio(void)
{
  PlayVerifyAdminPasswordAudio();
}

void Gtx31xPlayKeyComparisonErrorSound(void)
{
  PlayPasswordComparisonIsInconsistentAudio();
}

bool Gtx31xReportWakeup(void)
{
  return EventStartExternalModuleVerificationTask();
}

bool Gtx31xReportIntoStandby(void)
{
  return EventStartKeyScanfTask();
}

bool Gtx31xReportExitTyping(void)
{
  if (EventExitLocalRegisterKeyTask(KEY_TYPE_KEYBOARD, NULL, 0) == true)
    return true;

  if (EventExitVerifyNormalOpenPermissionTask(KEY_TYPE_KEYBOARD, NULL, 0) == true)
    return true;

  if (EventExitKeyInAdminPasswordTask(NULL, 0) == true)
    return true;

  if (EventExitKeyScanfTask(NULL, 0) == true)
    return true;

  return false;
}

bool Gtx31xGetPlaybackCompletionFlag(void)
{
  return EventGetVoicePlayBusyFlagBit();
}

bool Gtx31xGetExternalVerificationSensorEn(void)
{
#if _SAFETY_VERIFYFUNCTION_ENABLE_
#if _SAFETY_MODULE_TYPE_ == _FACE_SAFETY_MODULE_
  if (FaceGetTheNumberOfRegisteredUsers() > 0)
    return true;

#elif _SAFETY_MODULE_TYPE_ == FINGERPRINT_SAFETY_MODULE_
  if (FingerGetTheNumberOfRegisteredUsers() > 0)
    return true;

#elif _SAFETY_MODULE_TYPE_ == _QRCODE_SAFETY_MODULE_
  return true;
#endif
#endif

  return false;
}

void Gtx31xReportgSeltTestResult(bool Result)
{
  EventMarkingModuleSelfTestResults(KEYBOARD_DEV_SELF_TEST, Result);
}

bool Gtx31xVerifyAdminPasswordValidity(const void *pPassowrdIn, uint8_t NumberOfKey)
{
  if (NumberOfKey == NUMBER_OF_KEYBOARD_KEY)
  {
    uint8_t AdminPassword[NUMBER_OF_KEYBOARD_KEY] = {0};

    GetSuperAdministratorPassword(AdminPassword);
    if (memcmp(pPassowrdIn, AdminPassword, NUMBER_OF_KEYBOARD_KEY) == 0)
      return true;
  }

  return false;
}

void Gtx31xPreparationsBeforeLocalRegister(void)
{
  PlayEnterKeyViaTheKeyboardAudio();

  EventSetLocalRegisterKeyStep(LOCAL_REGISTER_ENTER_PASSWORD);
  EventUpdateLocalRegisterKeyResult(LOCAL_REGISTER_RESULT_BUSY);
}

void Gtx31xUpdateLocalEntryStatus(Gtx31xRegisterStep_t RegisterStep)
{
  switch (RegisterStep)
  {
  case GTX31X_REGISTER_END:
  {
    EventUpdateLocalRegisterKeyResult(LOCAL_REGISTER_RESULT_WAIT_SAVE);
  }
  break;

  case GTX31X_REGISTER_CONFIRM:
  {
    PlayAuthenticationKeyInconsistencyAudio();
    EventSetLocalRegisterKeyStep(LOCAL_REGISTER_REPEAT_PASSWORD);
  }
  break;

  default:
  {
    PlayEnterKeyViaTheKeyboardAudio();
    EventSetLocalRegisterKeyStep(LOCAL_REGISTER_ENTER_PASSWORD);
  }
  break;
  }
}

void Gtx31xReloadLocalRegisterTimer(void)
{
  EventResetTheRunningTimeOfSpecificAction(DEV_LOCAL_REGISTER);
}

bool Gtx31xGetDeviceBusyEn(void)
{
  if (EventGetLocalRegisterKeyStartEn() == true)
    return true;

  if (EventGetVerifyNormalOpenPermissionTaskStartEn() == true)
    return true;

  if (EventGetKeyInAdminPasswordTaskStartEn() == true)
    return true;

  return false;
}

bool Gtx31xReportUserTypedKeys(const void *pKeyValue, uint8_t NumberOfKey)
{
  if (EventExitVerifyNormalOpenPermissionTask(KEY_TYPE_KEYBOARD, pKeyValue, NumberOfKey) == true)
    return true;

  if (EventExitKeyInAdminPasswordTask(pKeyValue, NumberOfKey) == true)
    return true;

  if (EventExitLocalRegisterKeyTask(KEY_TYPE_KEYBOARD, pKeyValue, NumberOfKey) == true)
    return true;

  if (EventExitKeyScanfTask(pKeyValue, NumberOfKey) == true)
    return true;

  return false;
}

bool Gtx31xResetKeyTypedTimeout(void)
{
  if (EventResetTheRunningTimeOfSpecificAction(DEV_KEY_IN_ADMIN_PASSWORD) == true)
    return true;

  if (EventResetTheRunningTimeOfSpecificAction(DEV_VERIFY_NORMAL_OPEN_PERMISSION) == true)
    return true;

  if (EventResetTheRunningTimeOfSpecificAction(DEV_LOCAL_REGISTER) == true)
    return true;

  if (EventResetTheRunningTimeOfSpecificAction(DEV_KEYSCANF) == true)
    return true;

  return false;
}

void Gtx31xStopDeviceSelfTestTask(void)
{
  tmos_stop_task(Gtx31xEventTaskId, GTX31X_SELF_TEST);
}

void Gtx31xDelayedStartDeviceSelfTestTask(uint32_t Ms)
{
  tmos_stop_task(Gtx31xEventTaskId, GTX31X_SELF_TEST);
  tmos_start_task(Gtx31xEventTaskId, GTX31X_SELF_TEST, MS1_TO_SYSTEM_TIME(Ms));
}

void Gtx31xStartDeviceSelfTestTaskNoWait(void)
{
  tmos_stop_task(Gtx31xEventTaskId, GTX31X_SELF_TEST);
  tmos_set_event(Gtx31xEventTaskId, GTX31X_SELF_TEST);
}

void Gtx31xStopDataInteractionTask(void)
{
  tmos_stop_task(Gtx31xEventTaskId, GTX31X_DATA_INTERACTION);
}

void Gtx31xDelayedStartDataInteractionTask(uint32_t Ms)
{
  tmos_stop_task(Gtx31xEventTaskId, GTX31X_DATA_INTERACTION);
  tmos_start_task(Gtx31xEventTaskId, GTX31X_DATA_INTERACTION, MS1_TO_SYSTEM_TIME(Ms));
}

void Gtx31xStartDataInteractionTaskNoWait(void)
{
  tmos_stop_task(Gtx31xEventTaskId, GTX31X_DATA_INTERACTION);
  tmos_set_event(Gtx31xEventTaskId, GTX31X_DATA_INTERACTION);
}

void Gtx31xStopKeyscanfDiabledTimer(void)
{
  tmos_stop_task(Gtx31xEventTaskId, GTX31X_KEYSCANF_LOCKED_TIMER);
}

void Gtx31xStartKeyscanfDiabledTimer(uint32_t DisableTime)
{
  tmos_stop_task(Gtx31xEventTaskId, GTX31X_KEYSCANF_LOCKED_TIMER);
  tmos_start_task(Gtx31xEventTaskId, GTX31X_KEYSCANF_LOCKED_TIMER, MS1_TO_SYSTEM_TIME(DisableTime));
}

void Gtx31xMarkStartKeyscanfDiabledTimeout(void)
{
  tmos_stop_task(Gtx31xEventTaskId, GTX31X_KEYSCANF_LOCKED_TIMER);
  tmos_set_event(Gtx31xEventTaskId, GTX31X_KEYSCANF_LOCKED_TIMER);
}

void Gtx31xBoardInitialisation(Gtx31xExternalInterfaces_t *pExternalInterfaces)
{
  Gtx31xExternalInterfaces.IrqCallback = pExternalInterfaces->IrqCallback;
  Gtx31xExternalInterfaces.DeviceSelfTestTask = pExternalInterfaces->DeviceSelfTestTask;
  Gtx31xExternalInterfaces.DataInteractionTask = pExternalInterfaces->DataInteractionTask;
  Gtx31xExternalInterfaces.KeyboardDisableTimeout = pExternalInterfaces->KeyboardDisableTimeout;
  Gtx31xEventTaskId = TMOS_ProcessEventRegister(Gtx31xEventTaskCallback);

  Gtx31xSdaPinConfiguration();
  Gtx31xSclPinConfiguration();
  Gtx31xIrqPinConfiguration();
  EventAddNewEventHandler(&Gtx31xEventHandlerTable, Gtx31xEventHandlerCallback);
}

#endif
